package com.usian.controller;

import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

@Controller
@RequestMapping("/consumer")
public class ConUserController {
    @Autowired
    RestTemplate restTemplate;
    //从Nacos注册中心获取服务端的ip、端口、要调用的服务
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @GetMapping("findById")
    public User findById(Integer id){
        ServiceInstance instance = loadBalancerClient.choose("provider");
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/user/findById/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
}
